<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增会计科目')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-kmdm-add">
         <div class="form-group">    
                <label class="col-sm-3 control-label">科目年度：</label>
                <div class="col-sm-8">
                   <select name="nd" id="nd" class="form-control" required>
						<option th:each="nd : ${ndlist}" th:text="${nd}" th:value="${nd}" th:selected="${nd==year}"></option>
				   </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">上级科目代码：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="treeId" name="sjkmdm" type="hidden" th:value="${tGgKmdm?.kmdm}"/>
                        <input class="form-control" type="text" onclick="selectKmdmTree()" id="treeName" readonly="true" th:value="${tGgKmdm?.kmmc}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
           <div class="form-group">    
                <label class="col-sm-3 control-label">科目代码：</label>
                <div class="col-sm-8">
                    <input name="kmdm" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">科目名称：</label>
                <div class="col-sm-8">
                    <input name="kmmc" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">科目别名：</label>
                <div class="col-sm-8">
                    <input name="kmbm" class="form-control" type="text">
                </div>
            </div>
          
            <div class="form-group">    
                <label class="col-sm-3 control-label">科目类型 ：</label>
                <div class="col-sm-8">
                    <select name="kmlx" class="form-control" required>
					    <option value="1">资产</option>
						<option value="2">负债</option>
						<option value="3">净资产</option>
						<option value="4">收入</option>
						<option value="5">支出</option>
					</select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">借贷方向：</label>
                <div class="col-sm-8">
                    <select name="jdfx" class="form-control" required>
					    <option value="1">借</option>
						<option value="-1">贷</option>
					</select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">科目级别：</label>
                <div class="col-sm-8">
                    <input name="kmjb" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">科目结构：</label>
                <div class="col-sm-8">
                    <input name="kmjg" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">是否末级：</label>
                <div class="col-sm-8">
                    <select name="sfmj" class="form-control" required>
					    <option value="0">是</option>
						<option value="1">否</option>
					</select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">是否辅助核算单位：</label>
                <div class="col-sm-8">
                     <select name="fzhsdw" class="form-control" required>
					    <option value="0">无</option>
						<option value="1">会计科目-单位</option>
						<option value="2">会计科目-单位-预算科目</option>
						<option value="3">会计科目-预算科目</option>
					</select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/kmdm"
        $("#form-kmdm-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
            	var sjkmdm=$("input[name='sjkmdm']").val();
            	var kmdm=$("input[name='kmdm']").val();
            	var kmjb=$("input[name='kmjb']").val();
            	var kmjg=$("input[name='kmjg']").val();
            	if(!$.common.numValid(kmdm)){
        			$.modal.alertWarning("科目代码只能为数字");
        			return false;
        		}
            	if (sjkmdm!=''){
            		if (kmdm.substring(0,sjkmdm.length)!=sjkmdm){
            			$.modal.alertWarning("科目代码必须包含上级科目代码");
            			return false;
            		}
            	}
            	if(!$.common.numValid(kmjb)){
        			$.modal.alertWarning("科目级别只能为数字");
        			return false;
        		}
            	if(!$.common.numValid(kmjg)){
        			$.modal.alertWarning("科目结构只能为数字");
        			return false;
        		}
            	if (kmjg.length!=Number(kmjb)){
            		$.modal.alertWarning("科目级别与科目结构长度不一致");
        			return false;
            	}
            	var kmLen=0;
            	for (var i=0;i<kmjb;i++){
            		kmLen=kmLen+Number(kmjg.substring(i,i+1));
            	}
            	if (kmdm.length!=kmLen){
        			$.modal.alertWarning("科目代码长度不对");
        			return false;
        		}
                $.operate.save(prefix + "/add", $('#form-kmdm-add').serialize());
            }
        }

        /*会计科目-新增-选择父部门树*/
        function selectKmdmTree() {
            var options = {
                title: '会计科目选择',
                width: "380",
                url: prefix + "/selectKmdmTree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
               $("#treeId").val(body.find('#treeId').val());
               $("#treeName").val(body.find('#treeName').val());
               layer.close(index);
        }
    </script>
</body>
</html>